package cms.utils;

import cms.Application;
import cms.web.action.install.InstallManage;
import cms.web.filter.InitApplicationListener;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;


/**
 * CreateDirUtils扩展封装
 */
public class CreateDirUtils {
    private static final Logger logger = LogManager.getLogger(CreateDirUtils.class);

    /**
     * 1.创建数据库分表实体类文件
     * 2.创建论坛上传图片存储目录
     *
     * @param args
     */
    public static void createDirectoriesAndEntityFile(String[] args) {
        //创建数据库分表实体类文件
        CreateEntityFile.create();
        //是否由maven插件启动
        boolean isMaven = isMaven(args);
        if (!isMaven) {
            SpringApplication application = new SpringApplication(Application.class);
            application.addListeners(new InitApplicationListener());//注册监听器
            application.setAllowCircularReferences(Boolean.TRUE);//spring boot 2.6 允许循环引用
            application.run(args);
            //ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
            //注册监听器
            //context.addApplicationListener(new InitApplicationListener());
        } else {//maven命令执行
            //论坛外部目录
            createDirectories();
        }
    }

    /**
     * 生成轮胎上传图片存储外部文件夹
     */
    public static void createDirectories() {
        String defaultExternalDirectory = getExternalDirectory();
        //生成外部文件夹
        InstallManage installManage = new InstallManage();
        List<String> folderList = installManage.folderList();
        if (folderList != null && folderList.size() > 0) {
            for (String folderPath : folderList) {
                //生成文件目录
                Path path = Paths.get(defaultExternalDirectory + File.separator + StringUtils.replace(folderPath, "/", File.separator));
                if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {//目录不存在
                    try {
                        Files.createDirectories(path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 获取论坛 论坛外部目录
     *
     * @return
     */
    public static String getExternalDirectory() {
        String defaultExternalDirectory = "";
        String user_dir = System.getProperty("user.dir");
        String tmpdir = System.getProperty("java.io.tmpdir");
        logger.info("user_dir: " + user_dir);
        logger.info("ehcache tmpdir: " + tmpdir);

        //论坛外部目录
        Object externalDirectory = YmlUtils.getYmlProperty("application.yml", "bbs.externalDirectory");
        if (externalDirectory != null && !"".equals(externalDirectory.toString().trim())) {//如果已设置了论坛外部目录
            defaultExternalDirectory = externalDirectory.toString();
        } else {
            defaultExternalDirectory = user_dir + File.separator + "target" + File.separator + "bbs";
        }
        return defaultExternalDirectory;
    }

    /**
     * 是否由maven插件启动
     *
     * @param args
     * @return
     */
    public static boolean isMaven(String[] args) {
        boolean isMaven = false;//是否由maven插件启动
        if (args != null && args.length > 0) {
            List<String> list = Arrays.asList(args);
            if (list.contains("maven")) {
                isMaven = true;
            }
        }
        return isMaven;
    }

}
